package xyz.yuzegod.megawalls.util;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;

public class PremiumUtils
{
    private static final Gson GSON;
    private static final String BASE_URL = "http://localhost:8080/plugin?key=";
    
    public static boolean validate(final String key) {
        try {
            final BufferedReader reader = new BufferedReader(new InputStreamReader(new URL("http://localhost:8080/plugin?key=" + key).openStream(), Charset.forName("UTF-8")));
            final StringBuilder sb = new StringBuilder();
            int cp;
            while ((cp = reader.read()) != -1) {
                sb.append((char)cp);
            }
            reader.close();
            final JsonObject jsonObject = (JsonObject)PremiumUtils.GSON.fromJson(sb.toString(), (Class)JsonObject.class);
            return jsonObject.get("success").getAsBoolean();
        }
        catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    public static void main(final String[] args) {
        System.out.println("success: " + validate("a"));
    }
    
    static {
        GSON = new Gson();
    }
}
